<template>
  <div>
    <a-modal v-model:visible="visible" :width="800" title="新增租船人责任险" @cancel="cacelModel" :bodyStyle="bodyStyle"  @ok="handleOk">
      <a-form :model="formState" ref="formAdd" labelAlign="left" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-row :gutter="16">
        <a-col :span="9" :style="{paddingBottom: '10px'}">
          <a-form-item name="dragger" no-style>
            <a-upload-dragger v-model:fileList="fileList"
                              :max-count="1"
                              :action="action"
                              :data="{contractType: 'con_insurance_people'}"
                              :beforeUpload="beforeUpload"
                              @change="postFile"
                              name="file"
                              accept=".pdf">
              <div class="dragger-img">
                <img src="@/assets/images/ResourceContract/file.png" alt="">
              </div>
              <p class="ant-upload-text" :style="{color: '#2C3749'}">将文件拖到此处，或 <span :style="{color: '#0062AD'}">点击上传</span> </p>
              <p class="ant-upload-hint" :style="{color: '#6B7A8B', fontSize: '14px'}" >只支持pdf格式</p>
            </a-upload-dragger>
          </a-form-item>
        </a-col>
        <a-col :span="15">
          <div class="form-item-border">
            <a-form-item label="贸易主体" name="tradeBody" :rules="[{ required: true, message: '请选择贸易主体' }]">
              <a-select :bordered="false" v-model:value="formState.tradeBody" placeholder="请选择贸易主体" :options="tradeareas" />
            </a-form-item>
          </div>
          <div class="form-item-border">
            <a-form-item label="合同签订日期" name="signingDate" :rules="[{ type: 'string', required: true, message: '请输入合同签订日期' }]">
              <a-date-picker :locale="locale" :bordered="false" :style="{width: '100%'}" v-model:value="formState['signingDate']"  value-format="YYYY-MM-DD" />
            </a-form-item>
          </div>
            <div class="form-item-border" >
            <a-form-item label="被保险人" name="assured" :rules="[{ required: true, message: '请输入被保险人' }]">
              <a-input :bordered="false" v-model:value="formState.assured" placeholder="请输入被保险人"/>
            </a-form-item>
            </div>
          <div class="form-item-border">
            <a-form-item label="承保人" name="insurer" :rules="[{ required: true, message: '请输入承保人' }]">
              <a-input :bordered="false" v-model:value="formState.insurer" placeholder="请输入承保人"/>
            </a-form-item>
          </div>
          <div class="form-item-border">
            <a-form-item label="价格" name="price" :rules="[{ required: true, message: '请输入合同累计价格' }]">
              <a-input-number :bordered="false" v-model:value="formState.price" :style="{width: '100%'}" placeholder="请输入合同累计价格"  :min="1" >
                <template #addonAfter>
                  <a-select  style="width: 100px" v-model:value="formState.currency">
                    <a-select-option :value="1">美元</a-select-option>
                    <a-select-option :value="2">人民币</a-select-option>
                    <a-select-option :value="3">欧元</a-select-option>
                  </a-select>
                </template>
              </a-input-number>
            </a-form-item>
          </div>
          <div class="form-item-border">
            <a-form-item label="服务期限" name="servicePeriod" :rules="[{ required: true, message: '请输入服务期限' }]">
              <a-input-number v-model:value="formState.servicePeriod"  :bordered="false" :style="{width: '100%'}" addon-after="年" placeholder="请输入服务期限"  :min="1"  />
            </a-form-item>
          </div>
          <div class="form-item-border">
            <a-form-item label="合同到期时间" name="expirationDate" :rules="[{ required: true, message: '请输入合同到期时间' }]">
              <a-date-picker :locale="locale" :bordered="false"
                             :style="{width: '100%'}"
                             v-model:value="formState['expirationDate']"
                             value-format="YYYY-MM-DD" />
            </a-form-item>
          </div>
        </a-col>
      </a-row>
      </a-form>
      <template #footer>
        <div class="from-btn-right">
          <a-button key="back" class="border-btn" @click="cacelModel">取消</a-button>
          <a-button key="submit" @click="handleOk" :loading="confirmLoading" class="search-btn1">保存</a-button>
        </div>
      </template>
    </a-modal>
  </div>
</template>

<script setup>
import {ref, reactive, getCurrentInstance, defineEmits} from "vue";
const { proxy  } = getCurrentInstance()
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN';
import 'dayjs/locale/zh-cn';
import {BASE_URL} from "@/config/proxyIp";
import {message} from "ant-design-vue";
const visible = ref(false);
const isSubmit = ref(false)
const emit = defineEmits(['saveMemForm'])
const fileList = ref([])
const action = ref(`${BASE_URL}/con/common/uploadContractWithAnalysis`)
const bodyStyle = ref({})
// 贸易主体
const tradeareas = ref([
  {
    label: '国贸',
    value: 1
  },
  {
    label: '新贸',
    value: 2
  },
  {
    label: '国贸&新贸',
    value: 3
  },
])
const labelCol = ref(
    {
      style: {
        width: '100px'
      },
    },
)

const wrapperCol = ref({
  span: 20
})
const formState = reactive({
   contractName: '', // 合同名称
   contractNumber: '', // 合同编号
   tradeBody: [], // 贸易主体(国贸、新贸)
   signingDate: '', // 签订日期
   assured: '', // 被保险人
   insurer: '', // 承保人
   price: '', // 价格
   currency: 1, // 币种
   servicePeriod: '', // 服务期限(单位年)
   createDate: '', // 创建时间
   createBy: '', // 创建人
})
const formAdd = ref()
const showModal = () => {
  visible.value = true
  fileList.value = []
  console.log('formAdd', formAdd.value)
};
const cacelModel = () =>{
  visible.value = false
  isSubmit.value = false
  fileList.value = []
  formAdd.value.resetFields()
}
const confirmLoading = ref(false)
const handleOk = () => {
  if(formState.filePath) {
    formAdd.value.validateFields().then(() => {
      console.log('上传的formData', formState);
      confirmLoading.value = true
      proxy.$api.postAddInsurance(formState).then(res => {
        if (res.resultCode === 0) {
          visible.value = false;
          isSubmit.value = true
          confirmLoading.value = false
          emit('saveMemForm', isSubmit.value)
          message.success('新增备忘录成功')
          formAdd.value.resetFields()
        }
      })
    }).catch(err => {
      console.log('请输入', err)
    })
  } else {
    message.error('请上传合同原件')
  }

};
const postFile = (info) => {
  console.log('上传文件1', info)
  const status = info.file.status
  if (status === 'done') {
    formState.filePath = info.file.response.resultObject.path
  } else if (status === 'error') {
    message.error(`${info.file.name} 文件上传失败`);
  }
}
const beforeUpload = (file) => {
  console.log('上传文件2', file)
  let houzui = file.name.substring(file.name.lastIndexOf('.'), file.name.length).toLowerCase()
  let res = ['.pdf'].includes(houzui)
  if (!res) {
    fileList.value = []
    message.error('请上传 PDF 文件')
  }
  return res
}
// eslint-disable-next-line no-undef
defineExpose({
  showModal,
  handleOk
})
</script>

<style lang="scss" scoped>
.form-item-flex {
  display: flex;
  justify-content: space-between;
  .form-item-border {
    margin-right: 4px;
  }
}
.from-btn-right {
  height: 62px;
  line-height: 62px;
  background: #ffffff;
 // box-shadow: 0px -4px 9px 0px rgba(0,0,0,0.09);
}
.dragger-img {
  margin-top: 40%;
  img {
    width: 114px;
    height: 110px;
  }
}
</style>
